﻿using System;
using System.Collections.Generic;
using System.Drawing;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;
using TRicheControl.Interface.ControlAction;

namespace TRicheControl.WinformControl
{
    [ToolboxBitmap(typeof(Button))]
    public class RicheButton : Control
    {
        private ContentAlignment mTextAlign = ContentAlignment.MiddleCenter;
        public ContentAlignment TextAlign 
        {
            get
            {
                return mTextAlign;
            }
            set
            {
                this.mTextAlign = value;
            }
        }

        private Graphics mGraphics;
        public Graphics Graphics 
        { 
            get
            {
                if(base.IsDisposed)
                {
                    return null;
                }
                if (mGraphics == null)
                {
                    mGraphics = base.CreateGraphics();
                }
                return mGraphics;
            }
        }

        public RicheButton()
        {
        }
        
        protected override void OnPaint(PaintEventArgs e)
        {
            DrawButton();
        }

        #region Action

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            Refresh();
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            GraphicsStyle.Styles.ButtonStyle.DrawMouseEnterStyle(this.Graphics, this.Bounds);
            DrawString(this.Graphics);
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            GraphicsStyle.Styles.ButtonStyle.DrawMouseLeaveStyle(this.Graphics, this.Bounds);
            DrawString(this.Graphics);
            base.OnMouseLeave(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            SomeThingDown();
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            SomeThingUp();
            base.OnMouseUp(e);
        }

        protected override void OnGotFocus(EventArgs e)
        {
            SomeThingFousced();
            base.OnGotFocus(e);
        }

        protected override void OnLostFocus(EventArgs e)
        {
            SomeThingLostFousced();
            base.OnLostFocus(e);
        }

        #endregion

        #region IRicheButtonAction
        
        public new void SomeThingClick()
        {
            //throw new NotImplementedException();
        }

        public void SomeThingDown()
        {
            GraphicsStyle.Styles.ButtonStyle.DrawMouseDownStyle(this.Graphics, this.Bounds);
            DrawString(this.Graphics);
            
        }

        public void SomeThingUp()
        {
            GraphicsStyle.Styles.ButtonStyle.DrawMouseEnterStyle(this.Graphics, this.Bounds);
            DrawString(this.Graphics);
        }

        public void DrawButton()
        {
            GraphicsStyle.Styles.ButtonStyle.DrawMouseLeaveStyle(Graphics, Bounds);
            if (this.BackgroundImage != null)
            {
                GraphicsStyle.Styles.ButtonStyle.DrawBackGroundImage(Graphics, BackgroundImage, Bounds, BackgroundImageLayout);
                GraphicsStyle.Styles.ButtonStyle.DrawBackColor(Graphics, Bounds, Color.Transparent);
            }
            GraphicsStyle.Styles.ButtonStyle.DrawBorder(Graphics, Bounds);
            DrawString(Graphics);
        }

        public void SomeThingFousced()
        {
            GraphicsStyle.Styles.ButtonStyle.DrawGetFocuseStyle(this.Graphics, this.Bounds);
            DrawString(Graphics);
        }

        public void SomeThingLostFousced()
        {
            GraphicsStyle.Styles.ButtonStyle.DrawLostFocuseStyle(this.Graphics, this.Bounds);
            DrawString(Graphics);
        }

        private void DrawString(Graphics g)
        {
            StringFormat format = new StringFormat();
            format.Alignment = GraphicsStyle.GetStringAlignment(TextAlign, false);
            format.LineAlignment = GraphicsStyle.GetStringAlignment(TextAlign, true);
            g.DrawString(Text, Font, new SolidBrush(ForeColor), new Rectangle(0, 0, Width, Height), format);
        }

        #endregion
    }
}
